package com.lshj;

import com.hj.lib.tio.HjClientAioHandler;
import com.hj.lib.util.HjTioUtil;
import com.hj.lib.util.LogUtil;
import com.hj.lib.util.TioConfig;
import com.lshj.impl.HjTioImpl;
import com.lshj.impl.JxJsObj;
import com.teamdev.jxbrowser.chromium.BrowserContext;
import com.teamdev.jxbrowser.chromium.BrowserContextParams;
import com.teamdev.jxbrowser.chromium.JSValue;
import com.teamdev.jxbrowser.chromium.events.FinishLoadingEvent;
import com.teamdev.jxbrowser.chromium.events.LoadAdapter;
import com.teamdev.jxbrowser.chromium.javafx.BrowserView;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.stage.WindowEvent;
import potato.jx.crack.JxBrowserHackUtil;
import potato.jx.crack.JxVersion;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.io.File;

import static com.hj.lib.util.TioConfig.home;
import static com.hj.lib.util.TioConfig.jxmh;


public class LshjMain extends Application {
    public  static com.teamdev.jxbrowser.chromium.Browser browser = null;
    public  static Stage primaryStage = null;
    @Override
    public void start(final Stage primaryStage) {
        String dir = jxmh + File.separator +"lshjls";
        BrowserContext context = new BrowserContext(new BrowserContextParams(dir));
        browser = new com.teamdev.jxbrowser.chromium.Browser(context);
        BrowserView view = new BrowserView(browser);
        Scene scene = new Scene(new BorderPane(view), 700, 500);
        primaryStage.setScene(scene);
        primaryStage.initStyle(StageStyle.UNDECORATED);
        primaryStage.show();
        primaryStage.setMaximized(true);
        primaryStage.setAlwaysOnTop(false);
        HjTioUtil.startClient(new HjTioImpl());
        browser.addLoadListener(new LoadAdapter() {
            @Override
            public void onFinishLoadingFrame(FinishLoadingEvent event) {
                super.onFinishLoadingFrame(event);
                JSValue window = browser.executeJavaScriptAndReturnValue("window");
                JxJsObj jxJsObj = new JxJsObj();
                window.asObject().setProperty("jxJsObj", jxJsObj);
            }
        });
        System.out.println(home);
        browser.loadURL(home);
        primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
            public void handle(WindowEvent event) {
                browser.dispose();
                Platform.exit();
            }
        });
        LshjMain.primaryStage = primaryStage;
        try {
            if (HjClientAioHandler.FQQM_CMD != null && !"".equals(HjClientAioHandler.FQQM_CMD)) {
                String[] command = {HjClientAioHandler.FQQM_CMD};
                Runtime.getRuntime().exec(command);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        try {
            JxBrowserHackUtil.hack(JxVersion.V6_22);
            LshjMain.launch(args);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
